package com.xuchaotong.utils;

/**
 *
 * @author pengxu
 * @date 2018/8/22
 */
public enum OrderPayChannelEnum {

    WX_PAY(1,"WX_PAY","微信支付"),
    ALIPAY(1<<1,"ALIPAY","支付宝支付"), //2
    ACCOUNT(1<<2,"ACCOUNT","账户余额支付"),//4
    CARD_COUPON(1<<3,"CARD_COUPON","次数卡抵扣"),//8
    NO_NEED_PAY(1<<4,"NOT_NEED_PAY","到店支付"),//16
    WX_MICRO_PAY(1<<5,"WX_MICRO_PAY","微信刷卡支付"),//32
    CASHIER_BOARD_PAY(1<<6,"CASHIER_BOARD_PAY","收银台支付"),//64
    CASH_PAY(1<<7,"CASH_PAY","现金支付"),//128
    UNION_PAY(1<<8,"UNION_PAY","银联支付"),//256
    OTHER_APP_PAY(1<<9,"OTHER_APP_PAY","第三方应用支付"),//512
    WX_FACE_PAY(1 << 10,"WX_FACE_PAY","微信刷脸支付"),//1024
    YLINK_PAY(1<<11,"YLINK_PAY","碧合支付"),//2048
    OFFLINE_PAY(1<<12,"OFFLINE_PAY","线下协商")//4096
    ;
    private int code;

    private String value;

    private String desc;

    OrderPayChannelEnum(int code, String value, String desc){
        this.code = code;
        this.value = value;
        this.desc = desc;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static OrderPayChannelEnum getByCode(int code) {
        for (OrderPayChannelEnum type : OrderPayChannelEnum.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return null;
    }


    /**
     * 该支付渠道是否可以用来购买充值卡
     * @param payChannel
     * @return
     */
    public static boolean canByRechargeItem(int payChannel){
        return WX_PAY.code == payChannel || ALIPAY.code == payChannel || WX_MICRO_PAY.code == payChannel
                || CASH_PAY.code == payChannel || WX_FACE_PAY.code == payChannel;
    }

    /**
     * 该支付渠道是否要产生充值流水
     * @param payChannel
     * @return
     */
    public static boolean needGenerateRechargeBill(int payChannel){
        return WX_PAY.code == payChannel || ALIPAY.code == payChannel ||
                WX_MICRO_PAY.code == payChannel || CASH_PAY.code == payChannel;
    }

    public static OrderPayChannelEnum getByValue(String value){
        for (OrderPayChannelEnum type : OrderPayChannelEnum.values()) {
            if (type.getValue().equals(value)) {
                return type;
            }
        }
        return null;
    }

    public static OrderPayChannelEnum getByOldPayChannelEnum(int oldCode){
        switch (oldCode){
            case 1:
                return WX_PAY;
            case 2:
                return ALIPAY;
            case 3:
                return ACCOUNT;
            case 4:
                return CARD_COUPON;
            case 5:
                return NO_NEED_PAY;
            case 6:
                return WX_MICRO_PAY;
            case 7:
                return CASHIER_BOARD_PAY;
            case 8:
                return CASH_PAY;
            case 1 << 10 :
                return WX_FACE_PAY;
            case 1 << 11:
                return YLINK_PAY;
            default:
                return null;


        }

    }

    public static String getPayChannel(int payChannel){
        String payChannelStr = "";
        if((payChannel&WX_PAY.getCode())==WX_PAY.getCode()){
            payChannelStr+=WX_PAY.desc+" ";
        }
        if((payChannel&ALIPAY.getCode())==ALIPAY.getCode()){
            payChannelStr+=ALIPAY.desc+" ";
        }
        if((payChannel&ACCOUNT.getCode())==ACCOUNT.getCode()){
            payChannelStr+=ACCOUNT.desc+" ";
        }
        if((payChannel&CARD_COUPON.getCode())==CARD_COUPON.getCode()){
            payChannelStr+=CARD_COUPON.desc+" ";
        }
        if((payChannel&NO_NEED_PAY.getCode())==NO_NEED_PAY.getCode()){
            payChannelStr+=NO_NEED_PAY.desc+" ";
        }
        if((payChannel&WX_MICRO_PAY.getCode())==WX_MICRO_PAY.getCode()){
            payChannelStr+=WX_MICRO_PAY.desc+" ";
        }
        if((payChannel&CASHIER_BOARD_PAY.getCode())==CASHIER_BOARD_PAY.getCode()){
            payChannelStr+=CASHIER_BOARD_PAY.desc+" ";
        }
        if((payChannel&CASH_PAY.getCode())==CASH_PAY.getCode()){
            payChannelStr+=CASH_PAY.desc+" ";
        }
        if((payChannel&WX_FACE_PAY.getCode())== WX_FACE_PAY.getCode()){
            payChannelStr+=WX_FACE_PAY.desc+" ";
        }

        if((payChannel&YLINK_PAY.getCode())== YLINK_PAY.getCode()){
            payChannelStr+=YLINK_PAY.desc+" ";
        }
        return payChannelStr;
    }

    public static boolean isPayChannel(long payChannel, OrderPayChannelEnum payChannelEnum){
        return (payChannel & payChannelEnum.getCode()) == payChannelEnum.getCode();
    }


    public static void main(String[] args) {
        System.out.println(OrderPayChannelEnum.getPayChannel(1));
        System.out.println(OrderPayChannelEnum.getPayChannel(2));
        System.out.println(OrderPayChannelEnum.getPayChannel(4));
        System.out.println(OrderPayChannelEnum.getPayChannel(8));
    }
}
